﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TCM.Controllers
{
    public class ExpenseController : Controller
    {
        private TCMContext db = new TCMContext();

        //
        // GET: /Expense/

        public ActionResult Index()
        {
            var expensebases = db.EXPENSEBASEs.Include(e => e.EMPLOYEEBASE);
            return View(expensebases.ToList());
        }

        //
        // GET: /Expense/Details/5

        public ActionResult Details(Guid? id = null)
        {
            EXPENSEBASE expensebase = db.EXPENSEBASEs.Find(id);
            if (expensebase == null)
            {
                return HttpNotFound();
            }
            return View(expensebase);
        }

        //
        // GET: /Expense/Create

        public ActionResult Create()
        {
            ViewBag.RESPONSIBLEEMPLOYEE = new SelectList(db.EMPLOYEEBASEs, "EMPLOYEEID", "NAME");
            ViewBag.STATUSID = new SelectList(db.MAPSTRINGBASEs
                .Where<MAPSTRINGBASE>
                (x => x.ATTRIBUTENAME.Equals("statusid") &&
                    x.ENTITYNAME.Equals("EXPENSEBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME");
            return View();
        }

        //
        // POST: /Expense/Create

        [HttpPost]
        public ActionResult Create(EXPENSEBASE expensebase)
        {
            if (ModelState.IsValid)
            {
                expensebase.EXPENSEID = Guid.NewGuid();
                db.EXPENSEBASEs.Add(expensebase);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.RESPONSIBLEEMPLOYEE = new SelectList(db.EMPLOYEEBASEs, "EMPLOYEEID", "NAME", expensebase.RESPONSIBLEEMPLOYEE);

            ViewBag.STATUSID = new SelectList(db.MAPSTRINGBASEs
                .Where<MAPSTRINGBASE>
                (x => x.ATTRIBUTENAME.Equals("statusid") &&
                    x.ENTITYNAME.Equals("EXPENSEBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME");
            return View(expensebase);
        }

        //
        // GET: /Expense/Edit/5

        public ActionResult Edit(Guid? id = null)
        {
            EXPENSEBASE expensebase = db.EXPENSEBASEs.Find(id);
            if (expensebase == null)
            {
                return HttpNotFound();
            }
            ViewBag.RESPONSIBLEEMPLOYEE = new SelectList(db.EMPLOYEEBASEs, "EMPLOYEEID", "NAME", expensebase.RESPONSIBLEEMPLOYEE);
            ViewBag.STATUSID = new SelectList(db.MAPSTRINGBASEs
                .Where<MAPSTRINGBASE>
                (x => x.ATTRIBUTENAME.Equals("statusid") &&
                    x.ENTITYNAME.Equals("EXPENSEBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME", expensebase.STATUSID);
            return View(expensebase);
        }

        //
        // POST: /Expense/Edit/5

        [HttpPost]
        public ActionResult Edit(EXPENSEBASE expensebase)
        {
            if (ModelState.IsValid)
            {
                db.Entry(expensebase).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.RESPONSIBLEEMPLOYEE = new SelectList(db.EMPLOYEEBASEs, "EMPLOYEEID", "NAME", expensebase.RESPONSIBLEEMPLOYEE);
            ViewBag.STATUSID = new SelectList(db.MAPSTRINGBASEs
                .Where<MAPSTRINGBASE>
                (x => x.ATTRIBUTENAME.Equals("statusid") &&
                    x.ENTITYNAME.Equals("EXPENSEBASE")).ToList().AsEnumerable(), "MAPSTRINGID", "DISPLAYNAME", expensebase.STATUSID);
            return View(expensebase);
        }

        //
        // GET: /Expense/Delete/5

        public ActionResult Delete(Guid? id = null)
        {
            EXPENSEBASE expensebase = db.EXPENSEBASEs.Find(id);
            if (expensebase == null)
            {
                return HttpNotFound();
            }
            return View(expensebase);
        }

        //
        // POST: /Expense/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {
            EXPENSEBASE expensebase = db.EXPENSEBASEs.Find(id);
            db.EXPENSEBASEs.Remove(expensebase);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}